package com.wandian.weather.mvvm.nlmodern;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.TimeFormate;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.bean.HolidayEntity;
import com.wandian.weather.bean.HolidayListBean;
import com.wandian.weather.bean.YiJiDataBean;
import com.wandian.weather.data.BaseAppApi;
import com.wandian.weather.tools.JmTools;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description WeatherNLModernViewModel
 **/
public class WeatherNLModernViewModel extends BaseViewModel {
    private Retrofit retrofit;
    private BaseAppApi request;
    public MutableLiveData<YiJiDataBean> yjDataBean = new MutableLiveData<>();
    public MutableLiveData<List<HolidayEntity>> listHoliday = new MutableLiveData<>();
    public WeatherNLModernViewModel(@NonNull Application application) {
        super(application);
        //创建Retrofit对象
        retrofit = RetrofitUtil.createWeatherRetrofit();
        //创建网络请求接口实例
        request = retrofit.create(BaseAppApi.class);
    }


    public void getJijiari(){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.wnl.tianqijun.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("y", TimeFormate.getYear(System.currentTimeMillis()) + "");
        showDialog();
        Call<Object> call = request.postJJR(JmTools.encryptParams(hashMap, "Dayrl.Festival"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                dismissDialog();
                Log.d("postJJR=3", "postJJR="+JsonUtil.toJson(response.body()));
                if(!UIUtils.isEmpty(response) && !UIUtils.isEmpty(response.body())){
                    HolidayListBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(response.body()),HolidayListBean.class);
                    if(!UIUtils.isEmpty(bean) && !UIUtils.isEmpty(bean.getData())){
                        listHoliday.setValue(bean.getData());
                    }
                }else {
                    ToastUtil.show("网络错误");
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                dismissDialog();
                ToastUtil.show("网络错误");
            }
        });
    }


}
